package cn.edu.nuc.thrid.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.nuc.thrid.dto.PageInfo;
import cn.edu.nuc.thrid.model.Goods;
import cn.edu.nuc.thrid.services.interfaces.GoodsService;

@Controller
@RequestMapping("/goods")
public class AdminGoodsController {

	@Autowired
	private GoodsService service;

	/**
	 * 商品列表
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/goodlist", method = RequestMethod.GET)
	public String goodsList(Model model) {

		List<Goods> list = service.goodlist();
		// System.out.println(list);
		model.addAttribute("goodslist", list);
		return "goods/adminGoods";
	}

	/**
	 * 初始化修改内容
	 * 
	 * @param zybgoodid
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/editgood", method = RequestMethod.GET)
	public String editgood(Integer zybgoodid, Model model) {

		Goods good = service.initgood(zybgoodid);
		model.addAttribute("good", good);
		return "goods/adminEdit";
	}

	/**
	 * 修改
	 * 
	 * @param good
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String edit( Goods goods, Model model) {

		Integer result = service.updategood(goods);
		if (result > 0) {
			return "redirect:/goods/adminpagelist";
		} else {
			return "error";
		}
	}

	/**
	 * 查询
	 * 
	 * @param mes
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/searchgood", method = RequestMethod.POST)
	public String serch(String mes, Model model) {

		if (mes == null || "".equals(mes)) {

			return "redirect:/goods/adminpagelist";
		}

		PageInfo p = new PageInfo();
		List<Goods> list = service.searchgood(mes);
		p.setList(list);
		// System.out.println(list);
		model.addAttribute("p", p);
		return "goods/adminGoods";
	}

	/**
	 * 删除
	 * 
	 * @param zybgoodid
	 * @return
	 */
	@RequestMapping(value = "/deletegood", method = RequestMethod.GET)
	public String delete(Integer zybgoodid) {

		System.out.println("zybgoodid:" + zybgoodid);
		Integer result = service.deletegood(zybgoodid);
		if (result > 0) {

			return "redirect:/goods/adminpagelist";

		}
		return "error";
	}

	/**
	 * 添加跳转
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addgood", method = RequestMethod.GET)
	public String add1() {

		return "goods/adminAdd";
	}

	/**
	 * 添加
	 * 
	 * @param goods
	 * @param errors
	 * @return
	 */
	@RequestMapping(value = "/addgood", method = RequestMethod.POST)
	public String add2(@Valid Goods goods, Errors errors, Model model) {

		if (errors.hasErrors()) {
			System.out.println("校验添加商品");
			return "goods/adminAdd";
		}
		Integer result = service.addGood(goods);
		if (result > 0) {
			model.addAttribute("mes", "成功");
			return "goods/close";

		} else if (result == -1) {

			model.addAttribute("mes", "失败");
			return "goods/close";
		} else {
			return "error";
		}
	}

	/**
	 * ajax校验重名
	 * 
	 * @param goods
	 * @return
	 */
	@RequestMapping(value = "/valiname", method = RequestMethod.POST)
	public @ResponseBody Goods vali(@RequestBody Goods goods) {

		System.out.println("zybgoodname:" + goods.getZybgoodname());
		Goods good = service.valiname(goods.getZybgoodname());
		System.out.println("good:" + good);
		return good;

	}

	/**
	 * 分页显示
	 * 
	 * @param current
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/adminpagelist", method = RequestMethod.GET)
	public String pagegoodslist(@RequestParam(name = "current", defaultValue = "1") Integer current, Model model) {

		PageInfo pageInfo = service.pagelist(current);

		model.addAttribute("p", pageInfo);
		return "goods/adminGoods";
	}
}
